<template>
  <div id="bk" class="wrapper">
    <div class="login-box">
      <div class="title">
        <b>注 册</b>
      </div>
      <div style="margin-top: 30px">
        <el-form label-width="70px">
          <el-form-item label="用户名">
            <el-input
              v-model.trim="user.username"
              aria-required="true"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" style="margin-top: 25px">
            <el-input
              v-model.trim="user.password"
              show-password
              aria-required="true"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认密码" style="margin-top: 25px">
            <el-input
              v-model.trim="user.confirmPassword"
              show-password
              aria-required="true"
            ></el-input>
          </el-form-item>
          <el-form-item style="text-align: center;">
            <el-button
              type="success"
              @click="onSubmit"
              style="font-size: 22px"
            >
              注册
            </el-button>
            <el-button
              @click="$router.push('/login')"
              style="font-size: 22px"
            >
              返回
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import md5 from "js-md5";

export default {
  name: "UserRegister", // 组件名改为多单词形式
  data() {
    return {
      user: {},
    };
  },
  methods: {
    onSubmit() {
      if (
        this.user.username === "" ||
        this.user.password === "" ||
        this.user.confirmPassword === ""
      ) {
        this.$message.error("账号或密码不能为空");
        return false;
      }
      
      if (this.user.password !== this.user.confirmPassword) {
        this.$message.error("两次密码不一致");
        return false;
      }
      
      // 密码加密处理
      const encryptedPassword = md5(this.user.password);
      
      // 创建提交数据对象，避免修改原始数据
      const submitData = {
        ...this.user,
        password: encryptedPassword,
        confirmPassword: undefined // 不提交确认密码字段
      };
      
      this.request.post("/register", submitData).then((res) => {
        if (res.code === "200") {
          this.$message.success("注册成功");
          this.$router.push("/login");
        } else {
          this.$message.error(res.msg);
        }
      });
    },
  },
};
</script>

<style scoped>
.wrapper {
  height: 100vh;
  background-color: #006400;
  overflow: auto;
}

.login-box {
  margin: 220px auto;
  padding: 40px;
  width: 450px;
  height: 310px;
  background-color: #ffffff;
  border-radius: 10px;
}

.title {
  text-align: center;
  margin-top: 10px;
  font-size: 25px;
  color: #006400;
}

#bk {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  overflow-y: auto;
  height: 100%;
  background: url("../resource/01.jpg") center top / cover no-repeat;
}
</style>